mui.init();
mui('.mui-content .mui-switch').each(function() { //循环所有toggle

	//toggle.classList.contains('mui-active') 可识别该toggle的开关状态
	//	this.parentNode.querySelector('span').innerText = '状态：' + (this.classList.contains('mui-active') ? 'true' : 'false');
	/**
	 * toggle 事件监听
	 */
	this.addEventListener('toggle', function(event) {
		switch (this.id) {
			case 'mySwitch':
				var rank = event.detail.isActive ? 1 : 0;
				var url = UrlConfig.updateIsRank;
				var pars = {
					rank: rank
				};
				MuiEventMgr.ajax(url, {
					pars: pars,
					sfn: function(data) {
						//	  		var msg = data.status;
						//	  		if(msg=='true'){
						//	  			mui.alert("修改成功");
						//	  			mui.back();
						//	  		}else{
						//	  			mui.alert("修改失败")
						//	  		}
					},
					ffn: function(err) {
						mui.alert("获取数据失败");
					}
				});
				break;
				case 'lockerSwitch':
					if(event.detail.isActive){
						MuiEventMgr.open('../locker/locker.html', {});
					}else{
						var settings = app.getSettings();
						settings.gestures = '';
						settings.autoLogin = false;
						settings.overTime = 0;
						app.setSettings(settings);
					}
				break;
		}

		//event.detail.isActive 可直接获取当前状态
		//		this.parentNode.querySelector('span').innerText = '状态：' + (event.detail.isActive ? 'true' : 'false');
	});
});

mui(".mui-content ").on("tap", "a", function() {
	var targetPageId = plus.webview.currentWebview().id;
	plus.storage.setItem(KeyCfg.targetPageId, targetPageId);
	var self = this;
	var name = $(self).attr("id");
	switch (name) {
		case 'loginPwd':
			var realName = $("#realName1").val();
			var url = '';
			if (realName == '未设置') {
				url = '../srealName/srealName.html';
			} else {
				url = 'reset_login_pwd.html';
			}
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.checkSecuityForwad(function() {
					openPage(url, {});
				});
			},function(){
				var win = plus.webview.getWebviewById(appid);
				win.evalJS("openTab(null,subpages2[0]);");
				window.setTimeout(function(){win.show();},1000);
			});
			break;
		case 'tranPwdlink':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.checkSecuityForwad(function() {
					openPage('reset_detail_pwd.html', {});
				});
			},function(){
				var win = plus.webview.getWebviewById(appid);
				win.evalJS("openTab(null,subpages2[0]);");
				window.setTimeout(function(){win.show();},1000);
			});
			break;
		case 'emaillink':
			var email1 = $("#email1").val();
			var url = 'semail.html';
			if (email1 == '未设置') {
				url = 'semail.html';
			} else {
				url = 'reset_email.html';
			}
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.checkSecuityForwad(function() {
					openPage(url, {});
				});
			},function(){
				var win = plus.webview.getWebviewById(appid);
				win.evalJS("openTab(null,subpages2[0]);");
				window.setTimeout(function(){win.show();},1000);
			});
			break;
		case 'safelink':
			var safe1 = $("#safe1").val();
			var url = 'semail.html';
			if (safe1 == '未设置') {
				url = 'sproblem.html';
			} else {
				url = 'reset_safety_problem.html';
			}
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.checkSecuityForwad(function() {
					openPage(url, {});
				});
			},function(){
				var win = plus.webview.getWebviewById(appid);
				win.evalJS("openTab(null,subpages2[0]);");
				window.setTimeout(function(){win.show();},1000);
			});
			break;
		case 'phonelink':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.checkSecuityForwad(function() {
					openPage('reset_phone.html', {});
				});
			},function(){
				var win = plus.webview.getWebviewById(appid);
				win.evalJS("openTab(null,subpages2[0]);");
				window.setTimeout(function(){win.show();},1000);
			});
			break;
		case 'nickNamelink':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.checkSecuityForwad(function() {
					openPage('reset_name.html', {});
				});
			},function(){
				var win = plus.webview.getWebviewById(appid);
				win.evalJS("openTab(null,subpages2[0]);");
				window.setTimeout(function(){win.show();},1000);
			});
			break;
		case 'lockerlink':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.open('../locker/locker.html', {});
			},function(){
				var win = plus.webview.getWebviewById(appid);
				win.evalJS("openTab(null,subpages2[0]);");
				window.setTimeout(function(){win.show();},1000);
			});
			break;
		case 'myBanklink':
			MuiEventMgr.checkSessionByRemote(function(){
				MuiEventMgr.checkSecuityForwad(function(){
					openPage('../mybank/mybank.html',{});	
				});
			},function(){
				var win = plus.webview.getWebviewById(appid);
				win.evalJS("openTab(null,subpages2[0]);");
				window.setTimeout(function(){win.show();},1000);
			});
			break;
	}
});

mui(".mui-popover ").on("tap", "a", function() {
	var targetPageId = plus.webview.currentWebview().id;
	plus.storage.setItem(KeyCfg.targetPageId, targetPageId);
	var self = this;
	var name = $(self).attr("id");
	switch (name) {
		case 'reset_login_pwd':
			MuiEventMgr.open('reset_login_pwd.html', {});
			break;
		case 'reset_detail_pwd':
			MuiEventMgr.checkSecuityForwad(function() {
				openPage('reset_detail_pwd.html', {});
			});
			break;
		case 'reset_email':
			{
				var email1 = $("#email1").val();
				var url = 'semail.html';
				if (email1 == '未设置') {
					url = 'semail.html';
				} else {
					url = 'reset_email.html';
				}
				MuiEventMgr.checkSecuityForwad(function() {
					openPage(url, {});
				});
			}
		case 'reset_safe':
			{
				var safe1 = $("#safe1").val();
				var url = 'semail.html';
				if (safe1 == '未设置') {
					url = 'sproblem.html';
				} else {
					url = 'reset_safety_problem.html';
				}
				MuiEventMgr.checkSecuityForwad(function() {
					openPage(url, {});
				});
				break;
			}
		case 'reset_phone':
			MuiEventMgr.checkSecuityForwad(function() {
				openPage('reset_phone.html', {});
			});
			break;
		case 'reset_name':
			MuiEventMgr.checkSecuityForwad(function() {
				openPage('reset_name.html', {});
			});
			break;
		
	}
});


/**
 * 打开新页面
 * @param {Object} url	新地址
 * @param {Object} pars	参数
 */
function openPage(url, pars) {
	if (!pars) pars = {};
	var cfg = {
		pars: pars
	};
	MuiEventMgr.openBySafe(url, cfg);
}

/**
 * 回调用当前“刷新”事件
 */
window.addEventListener('reload', function(event) {
	loadDatas();
});


mui.plusReady(function() {
	loadDatas();
	var settings = app.getSettings();
	if (settings.autoLogin && settings.gestures){
		document.getElementById("lockerSwitch").classList.add('mui-active');
	}
	(function() {
		var targetPageId = plus.webview.currentWebview().id;
		plus.storage.setItem(KeyCfg.targetPageId, targetPageId);
		var level = CmwMcore.$('level');
		MeventMgr.onTouchstart(level, function(event, self) {
			MuiEventMgr.checkSecuity();
		});
	})();
});





function loadDatas() {
	var url = UrlConfig.getUserInfo;
	var guid = plus.storage.getItem(KeyCfg.userId);
	var pars = {
		guid: guid
	};
	// 弹出系统等待对话框   
	//var w = mui.plus.nativeUI.showWaiting( "正在加载..." );
	MuiEventMgr.ajax(url, {
		pars: pars,
		sfn: function(data) {
			callback(data);
		},
		ffn: function(err) {
			mui.alert("获取数据失败：[" + err + "]");
			//mui.plus.nativeUI.closeWaiting();
		}
	});
}

function callback(data) {
	$(".mui-loading").hide();
	var user = data;
	$("#username").val(user.userName);
	var safe = data.safe;
	var realName = user.realName;
	var card = user.card;
	var status = user.isRank;
	var transPassword = user.transPassword;
	$("#prompt").hide();
	if (transPassword)
		$("#tranPwd").html("******");
	else
		$("#tranPwd").html("未设置");
	if (status) {
		var mySwitch = document.getElementById("mySwitch");
		mySwitch.classList.add('mui-active');
	} else {
		var mySwitch = document.getElementById("mySwitch");
		mySwitch.classList.remove('mui-active');
	}
	if (!realName) {
		realName = "未设置";
	} else {
		realName = realName.substring(0, 1) + "**";
	}
	if (safe == true) {
		safe = "已设置";
	} else {
		safe = "未设置";
	}
	if (!card) {
		card = "未设置";
	} else {
		card = card.substring(0, 4) + "***********" + card.substring(card.length - 4, card.length);
	}
	var nickName = user.nickName;
	if (!nickName) {
		nickName = "未设置";
	}
	//	var isRank = user.isRank
	//	if(isRank==true){isRank="参加";}
	//	else{isRank="不参加";}
	var phone = user.phone;
	if (!phone) {
		phone = "未设置";
	} else {
		phone = phone.substring(0, 3) + "****" + phone.substring(phone.length - 4, phone.length);
	}
	var email = user.email;
	if (!email) {
		$("#prompt").show();
		email = "未设置";
	}else{
		email = "*******" + email.substring(email.indexOf('@')-2, email.length);
	}
	var guid = user.guid;
	var url = UrlConfig.muser_register + '/security/register.htm?activeId=' + guid;
	//	$("#promote").html("推广链接："+url);
	$("#realName").html(realName);
	$("#card").html(card);
	$("#email").html(email);
	$("#safe").html(safe);
	$("#phone").html(phone);
	$("#nickName").html(nickName);
	$("#status").val(status);

	$("#realName1").html(realName);
	$("#email1").val(email);
	$("#safe1").val(safe);
	$("#phone1").val(phone);
	$("#nickName1").val(nickName);
	$("#tranPwd1").val(transPassword);





	//	var html=getHtml(data);
	//	$("#item1_loanRecords").html(html);
	//	addClick();

}

function getHtml(data) {
	var user = data;
	$("#username").val(user.userName);
	var safe = data.safe;
	var realName = user.realName;
	var card = user.card;
	var status = user.isRank;
	$("#status").val(status);

	if (!realName) {
		realName = "未设置";
	} else {
		realName = realName.substring(0, 1) + "**";
	}
	if (safe == 'true') {
		safe = "已设置";
	} else {
		safe = "未设置";
	}
	if (!card) {
		card = "未设置";
	} else {
		card = card.substring(0, 4) + "***********" + card.substring(card.length - 4, card.length);
	}
	var nickName = user.nickName;
	if (!nickName) {
		nickName = "未设置";
	}
	//	var isRank = user.isRank
	//	if(isRank=="1"){isRank="参加";}
	//	else{isRank="不参加";}
	var phone = user.phone;
	if (!phone) {
		phone = "未设置";
	} else {
		phone = phone.substring(0, 3) + "****" + phone.substring(phone.length - 4, phone.length);
	}
	var email = user.erroremial;
	if (!email) {
		email = "未设置"
	}
	var htmls = [
		'<li  class="mui-table-view-cell">真实姓名：' + realName + '</a></li>',
		'<li  class="mui-table-view-cell">身份证号：' + card + '</a></li>',
		'<li  class="mui-table-view-cell">',
		'	<a id="loginPwd" href="javascript:void(0)" class="mui-navigate-right">登录密码：******</a>',
		'</li>',
		'<li  class="mui-table-view-cell">',
		'	<a id="tranPwd" href="javascript:void(0)" class="mui-navigate-right">交易密码：******</a>',
		'</li>',
		'<li  class="mui-table-view-cell">',
		'	<a id="email" href="javascript:void(0)" class="mui-navigate-right">电子邮箱：' + email + '</a>',
		'</li>',
		'<li  class="mui-table-view-cell">',
		'	<a id="safe" href="javascript:void(0)" class="mui-navigate-right">安全问题：' + safe + '</a>',
		'</li>',
		'<li  class="mui-table-view-cell">',
		'	<a id="phone" href="javascript:void(0)" class="mui-navigate-right">手机号码：' + phone + '</a>',
		'</li>',
		'<li class="mui-table-view-cell">',
		'	<a id="nickName" href="javascript:void(0)" class="mui-navigate-right">昵称：' + nickName + '</a>',
		'</li>',
		'<li class="mui-table-view-cell">',
		'	<span>是否参加投资排行榜：</span>',
		'   <div id="mySwitch" class="mui-switch mui-active">',
		'		<div class="mui-switch-handle"></div>',
		'	</div>',
		'</li>',
		'<li class="mui-table-view-cell">',
		'	<a id="promote" href="javascript:void(0)" class="mui-navigate-right">推广链接：</a>',
		'</li>'
	];

	return htmls.join(" ");
}
